package master;

/**
 *
 * 生成螺旋哦矩阵
 *
 * @author chenyw
 * @date 2022/7/28 16:03
 */
public class M59generateMatrix {
    public static void main(String[] args) {
        new M59generateMatrix().generateMatrix(3);
    }
    public int[][] generateMatrix(int n) {
        int[][] matrix = new int[n][n];
        int top = 0, down = n - 1;
        int left = 0, right = n - 1;
        int count = 1;
        while (top <= down && left <= right) {
            for (int i = left; i <= right; i++) {
                matrix[top][i] = count;
                System.out.println(count);
                count++;
            }
            top++;
            System.out.println("top" + top);
            for (int i = top; i <= down; i++) {
                matrix[i][right] = count;
                System.out.println(count);
                count++;
            }
            right--;
            System.out.println("right" + right);
            for (int i = right; i >= left; i--) {
                matrix[down][i] = count;
                System.out.println(count);
                count++;
            }
            down--;
            System.out.println("down" + down);
            for (int i = down; i >= top; i--) {
                matrix[i][left] = count;
                System.out.println(count);
                count++;
            }
            left++;
            System.out.println("left" + left);
        }
        return matrix;
    }
}
